using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DBSourceToolsLib;
using DBConnectionLib;

namespace DBSourceToolsWizard
{
    public partial class Form1 : Form
    {
        private bool m_bDatabasesSelected = false;
        private bool m_bTargetDirSelected = false;
        private bool m_bBinDirSelected = false;
        private string m_strScriptTableOptionsXmlFile;
        private string m_strScriptProcOptionsXmlFile;
        private string m_strViewOptionsXmlFile;

        public Form1()
        {
            InitializeComponent();
        }

        private void m_btn_DatabasesXml_Click(object sender, EventArgs e)
        {
            if (this.m_openFileDialog.ShowDialog(this) == DialogResult.OK)
            {
                this.m_tb_DatabasesFile.Text = this.m_openFileDialog.FileName;

                DataSet oSourceDbListDataSet = new DataSet();
                oSourceDbListDataSet.ReadXml(this.m_tb_DatabasesFile.Text);

                this.m_cb_SourceDatabase.DataSource = oSourceDbListDataSet.Tables[0];
                this.m_cb_SourceDatabase.DisplayMember = "NickName";

                DataSet oTargetDbListDataSet = new DataSet();
                oTargetDbListDataSet.ReadXml(this.m_tb_DatabasesFile.Text);

                //this.m_cb_TargetDatabase.DataSource = oTargetDbListDataSet.Tables[0];
                //this.m_cb_TargetDatabase.DisplayMember = "NickName";

                this.m_listBox_TargetDatabases.DataSource = oTargetDbListDataSet.Tables[0];
                this.m_listBox_TargetDatabases.DisplayMember = "NickName";

                this.m_bDatabasesSelected = true;

            }
        }

        private void m_btn_TargetDirectory_Click(object sender, EventArgs e)
        {
            if (this.m_folderBrowserDialog.ShowDialog(this) == DialogResult.OK)
            {
                this.m_tb_TargetDir.Text = this.m_folderBrowserDialog.SelectedPath;
                DBSourceToolsLib.UserSettings.Instance().ProjectBaseDirectory = this.m_tb_TargetDir.Text;
                //DBSourceToolsLib.UserSettings.Instance().LoadDatabasesEx(this.m_tb_TargetDir.Text,
                //    this.m_tb_DatabasesFile.Text);

                string strBaseDir = this.m_tb_DatabasesFile.Text;
                strBaseDir = strBaseDir.Substring(0,
                    strBaseDir.LastIndexOf("\\"));

                this.m_strScriptTableOptionsXmlFile = strBaseDir + "\\TableScriptingOptions.xml";

                this.m_strScriptProcOptionsXmlFile = strBaseDir + "\\ProcScriptingOptions.xml";

                this.m_strViewOptionsXmlFile = strBaseDir + "\\ViewScriptingOptions.xml";


                this.m_bTargetDirSelected = true;
                EnableButtons();

            }

        }

        private void EnableButtons()
        {
            if (this.m_bDatabasesSelected && this.m_bTargetDirSelected)
                this.btnOptions.Enabled = true;
            if (this.m_bDatabasesSelected && this.m_bTargetDirSelected && this.m_bBinDirSelected)
                this.m_btn_Go.Enabled = true;

        }

        private void m_btn_Go_Click(object sender, EventArgs e)
        {

            if (this.m_listBox_TargetDatabases.SelectedIndices.Count < 1)
            {
                MessageBox.Show("Please select a target database");
            }
            else
            {

                try
                {
                    this.Cursor = Cursors.WaitCursor;

                    DatabaseEx oSourceDatabase
                        = DBSourceToolsLib.UserSettings.Instance().getSourceDatabase(this.m_cb_SourceDatabase.Text);

                    if (oSourceDatabase.SQLVersion.CompareTo("SQL7") == 0)
                    {
                        if (!oSourceDatabase.TestOleDbConnection())
                            throw new Exception(oSourceDatabase.ErrorMessage);
                    }
                    else
                    {
                        if (!oSourceDatabase.TestSqlClientConnection())
                            throw new Exception(oSourceDatabase.ErrorMessage);
                    }

                    oSourceDatabase.BaseDirectory = this.m_tb_TargetDir.Text + @"\" + oSourceDatabase.NickName;

                    List<DBConnectionLib.Database> lTargetList = new List<Database>();

                    for (int selItem = 0; selItem < this.m_listBox_TargetDatabases.SelectedIndices.Count; selItem++)
                    {
                        int selIndex = this.m_listBox_TargetDatabases.SelectedIndices[selItem];
                        DataRowView oSelectedItem = (DataRowView)this.m_listBox_TargetDatabases.Items[selIndex];
                        string sDbNickName = oSelectedItem["NickName"].ToString();
                        DBConnectionLib.Database oTargetDatabase
                            = DBSourceToolsLib.UserSettings.Instance().getSourceDatabase(oSelectedItem["NickName"].ToString());
                        oTargetDatabase.BaseDirectory = this.m_tb_TargetDir.Text;
                        lTargetList.Add(oTargetDatabase);
                    }

                    if (!this.m_checkBox_LoadFromDisk.Checked)
                    {
                        //DBSourceToolsLib.Commands.DatabaseCommand oDBLoadDb =
                        //    new DBSourceToolsLib.Commands.DatabaseCommand(this,
                        //        DBSourceToolsLib.Commands.DatabaseCommandTypes.LoadFromDatabase);
                        //oDBLoadDb.Execute(ref oSourceDatabase, false);
                    }
                    else
                    {
                        //DBSourceToolsLib.Commands.DatabaseCommand oDBLoadFromDisk =
                        //    new DBSourceToolsLib.Commands.DatabaseCommand(this, 
                        //        DBSourceToolsLib.Commands.DatabaseCommandTypes.LoadFromDisk);
                        //oDBLoadFromDisk.Execute(ref oSourceDatabase, false);

                    }

                    foreach (Database oTargetDatabase in lTargetList)
                    {
//                        oSourceDatabase.SysObjects.AddDeploymentTarget(oTargetDatabase);
                    }

                    //oSourceDatabase.SysObjects.ScriptOptions.ScriptData = this.m_b_Data.Checked;
                    //oSourceDatabase.SysObjects.ScriptOptions.ScriptDropAndCreate = this.m_b_DropAndCreate.Checked;
                    //oSourceDatabase.SysObjects.ScriptOptions.ScriptStoredProcs = this.m_b_StoredProcs.Checked;
                    //oSourceDatabase.SysObjects.ScriptOptions.ScriptTables = this.m_b_Tables.Checked;
                    //oSourceDatabase.SysObjects.ScriptOptions.ScriptDatabasesXmlFile = this.m_tb_DatabasesFile.Text;
                    //oSourceDatabase.SysObjects.ScriptOptions.ScriptViews = this.m_b_ScriptViews.Checked;
                    //oSourceDatabase.SysObjects.ScriptOptions.BinDir = this.m_tb_BinDir.Text;
                    //oSourceDatabase.SysObjects.ScriptOptions.LoadTableScriptOptionsFile(this.m_strScriptTableOptionsXmlFile);
                    //oSourceDatabase.SysObjects.ScriptOptions.LoadProcsOptionsFile(this.m_strScriptProcOptionsXmlFile);
                    //oSourceDatabase.SysObjects.ScriptOptions.LoadViewOptionsFile(this.m_strViewOptionsXmlFile);

                    // delete existing directories

                    //DBSourceToolsLib.Commands.DatabaseCommand oWriteToDisk
                    //    = new DBSourceToolsLib.Commands.DatabaseCommand(this,
                    //        DBSourceToolsLib.Commands.DatabaseCommandTypes.WriteToDisk);
                    //oWriteToDisk.Execute(ref oSourceDatabase, false);

                    this.Cursor = Cursors.Default;

                    MessageBox.Show("Export to disk successful");
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            }
        }

        private void m_tb_DatabasesFile_TextChanged(object sender, EventArgs e)
        {

        }

        private void label4_Click(object sender, EventArgs e)
        {

        }

        private void btnBinDir_Click(object sender, EventArgs e)
        {
            if (this.m_folderBrowserDialog.ShowDialog(this) == DialogResult.OK)
            {
                this.m_tb_BinDir.Text = this.m_folderBrowserDialog.SelectedPath;

                this.m_bBinDirSelected = true;
                EnableButtons();

            }

        }

        private void btnOptions_Click(object sender, EventArgs e)
        {

            DBSourceToolsLib.UI.Forms.TableOptionsForm oTableOptionsForm
                = new DBSourceToolsLib.UI.Forms.TableOptionsForm(
                    this.m_cb_SourceDatabase.Text, this.m_strScriptTableOptionsXmlFile
                    );

            oTableOptionsForm.Show(this);
       }

        private void btnProcOptions_Click(object sender, EventArgs e)
        {
            DBSourceToolsLib.UI.Forms.StoredProcOptionsForm oStoredProcForm = 
                new DBSourceToolsLib.UI.Forms.StoredProcOptionsForm(
                    this.m_cb_SourceDatabase.Text, this.m_strScriptProcOptionsXmlFile
                    );

            oStoredProcForm.Show(this);

        }

        private void m_b_StoredProcs_CheckedChanged(object sender, EventArgs e)
        {
            if (this.m_b_StoredProcs.Checked == true)
            {
                this.m_btnProcOptions.Enabled = true;
            }
            else
            {
                this.m_btnProcOptions.Enabled = false;
            }
        }

        private void btnViews_Click(object sender, EventArgs e)
        {
            DBSourceToolsLib.UI.Forms.ViewOptionsForm oViewOptionForm =
                new DBSourceToolsLib.UI.Forms.ViewOptionsForm(
                    this.m_cb_SourceDatabase.Text, this.m_strViewOptionsXmlFile
                    );

            oViewOptionForm.Show(this);

        }

        private void m_cb_ScriptViews_CheckedChanged(object sender, EventArgs e)
        {
            if (this.m_b_ScriptViews.Checked)
                this.btnViews.Enabled = true;
            else
                this.btnViews.Enabled = false;
        }
    }
}